<?php
namespace app\common;

use think\db\exception\DataNotFoundException;
use think\db\exception\ModelNotFoundException;
use think\exception\DbException;
use think\Model;

class BaseModel extends Model {
    /**
     * 新增
     * @auditor wws
     * @date 2020/12/20 16:32
     */
    public function add($data = []) {
        if(empty($data)) return 0;

        return db($this->table)->insertGetId($data);
    }

    /**
     * 批量新增
     * @auditor wws
     * @date 2020/12/20 16:32
     */
    public function addAll($data = []){
        if(empty($data)) return 0;

        return db($this->table)->insertAll($data);
    }

    /**
     * 更新
     * @auditor wws
     * @date 2020/12/20 16:32
     */
    public function edit($where = [], $update = []) {
        if(empty($where)) return false;
        $where['is_del'] = 0;

        return db($this->table)->where($where)->update($update);
    }

    /**
     * 删除
     * @auditor wws
     * @date 2020/12/20 16:32
     */
    public function del($where = []){
        if(empty($where)) return 0;
        $where['is_del'] = 0;

        return db($this->table)->where($where)->delete();
    }

    /**
     * 获取基础查询
     * @access public
     * @param array $params
     * @return array
     * +----------------------------------------------------------
     * | ******************* 参数示例 *******************
     * +----------------------------------------------------------
     * $queryParams = [
         * 'queryMode' => 'paging',                         [查询方式：info|详情，list|列表，paging|分页，column|获取单列或多列字段值]
         * 'alias'     => 'a',                              [别名]
         * 'field'     => 'outstock_id,outstock_sn',        [查询字段]
         * 'where'     => ['outstock_id' => 999],           [查询条件]
         * 'order'     => ['outstock_id' => 'asc'],         [排序条件]      $params['group']     [分组字段]
         * 'perpage'   => 30,                               [每页条数]
         * 'join'      => [                                 [连接条件：必须为二维数组]
             * [
             * 'sales_outstock_goods b',
             * 'b.outstock_id = a.outstock_id',
             * 'LEFT'
             * ],
             * [
             * 'inventory_goods c',
             * 'c.company_id = a.company_id and c.goods_id = b.goods_id',
             * 'LEFT'
             * ],
         * ],
     * ];
     * +----------------------------------------------------------
     * @throws DataNotFoundException
     * @throws ModelNotFoundException
     * @throws DbException
     */
    public function getBaseQuery($params){
        $query = db($this->table);
        // 别名
        if (isset($params['alias']) && !empty($params['alias'])) {
            $query = $query->alias($params['alias']);
        }
        // 查询字段
        if (isset($params['field']) && !empty($params['field'])) {
            $query = $query->field($params['field']);
        }
        // 查询条件
        if (isset($params['where']) && !empty($params['where'])) {
            $query = $query->where($params['where']);
        }
        // 排序条件
        if (isset($params['order']) && !empty($params['order'])) {
            $query = $query->order($params['order']);
        }
        // 分组条件
        if (isset($params['group']) && !empty($params['group'])) {
            $query = $query->group($params['group']);
        }
        // 连接条件
        if (isset($params['join']) && !empty($params['join'])) {
            if (!isset($params['alias']) || empty($params['alias'])) {
                return [];
            }
            // 格式必须为二维数组
            if (count($params['join']) == count($params['join'], 1)) {
                return [];
            }
            foreach ($params['join'] as $key => $value) {
                $query = $query->join($value[0], $value[1], $value[2]);
            }
        }

        // 区分查询方式
        if (isset($params['queryMode']) && !empty($params['queryMode'])) {
            switch ($params['queryMode']) {
                // 详情
                case 'info':
                    $data = $query->find();
                    break;
                // 列表
                case 'list':
                    $data = $query->select();
                    break;
                // 分页
                case 'paging':
                    if (!isset($params['perpage']) || empty($params['perpage'])) {
                        return [];
                    }
                    $data = $query->paginate($params['perpage'])->toArray();
                    break;
                // 获取单列或多列字段值
                case 'column':
                    if (!isset($params['field']) || empty($params['field'])) {
                        return [];
                    }
                    $data = $query->column($params['field']);
                    break;
                default:
                    return [];
                    break;
            }
        }else{
            return [];
        }

        if (is_null($data) || empty($data)) {
            return [];
        }

        return $data;
    }
}